package com.example.springboot.controller;

import com.example.springboot.entity.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 演示控制器
 */
@Api(tags = "演示接口")
@RestController
@RequestMapping("/demo")
public class DemoController {

    @ApiOperation(value = "基本GET请求示例", notes = "演示基本的GET请求")
    @GetMapping("/hello")
    public Result<String> hello() {
        return Result.success("Hello, Swagger!");
    }

    @ApiOperation(value = "带路径参数的GET请求", notes = "演示带路径参数的GET请求")
    @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String", paramType = "path")
    @GetMapping("/hello/{name}")
    public Result<String> helloWithName(@PathVariable String name) {
        return Result.success("Hello, " + name + "!");
    }

    @ApiOperation(value = "带查询参数的GET请求", notes = "演示带查询参数的GET请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/query")
    public Result<String> queryParams(@RequestParam(required = false) String name,
                                      @RequestParam(required = false) Integer age) {
        StringBuilder result = new StringBuilder("查询参数：");
        if (name != null) {
            result.append("姓名=").append(name);
        }
        if (age != null) {
            result.append(", 年龄=").append(age);
        }
        return Result.success(result.toString());
    }

    @ApiOperation(value = "基本POST请求", notes = "演示基本的POST请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "form")
    })
    @PostMapping("/login")
    public Result<String> login(@RequestParam String username, @RequestParam String password) {
        if ("admin".equals(username) && "123456".equals(password)) {
            return Result.success("登录成功");
        } else {
            return Result.error(401, "用户名或密码错误");
        }
    }

    @ApiOperation(value = "文件上传示例", notes = "演示文件上传接口的文档")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "__file", paramType = "form"),
            @ApiImplicitParam(name = "description", value = "文件描述", dataType = "String", paramType = "form")
    })
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam MultipartFile file) {
        // 实际上传逻辑省略，仅展示Swagger文档
        return Result.success("文件上传成功");
    }
} 